package com.bjsxt.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.controller.BaseController;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.common.util.PageInfoUtil;
import com.bjsxt.users.pojo.AAdmins;
import com.bjsxt.users.service.IUsersService;

@Controller
@RequestMapping("/back/admins/")
public class AdminsBackController extends BaseController{
	@Resource
	private IUsersService usersService;
	//登陆后首页
	@RequestMapping("index.htm")
	public String indext(){
	
		return "/back/index";
	}
	//左侧菜单
	@RequestMapping("leftMenu.htm")
	public String leftMenu(){
		return "back/leftMenu";
	}
	@RequestMapping("/adminsList.htm")
	public String adminsList(HttpServletRequest request){
		ConstatFinalUtil.LOGGER_SYSTEM.info("-- adminsList ---");
		//接受参数
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");
		
		if(st==null){
			st="";
		}
		if(ed==null){
			ed="";
		}
		Date stDate=null;
		Date edDate=null;
		PageInfoUtil pageInfoUtil=this.proccedPageInfo(request);
		Map<String, Object> condMap = new HashMap<String, Object>();
		if(!"".equalsIgnoreCase(st)&&!"".equalsIgnoreCase(ed)){
			stDate=this.dateFormatUtil.parseDateTime(st);
			edDate=this.dateFormatUtil.parseDateTime(ed);
			condMap.put("stDate", stDate);
			condMap.put("edDate", edDate);
			
		}
		//查询数据
	
		condMap.put("keyword", keyword);
		condMap.put("status", status);
		
		List<AAdmins> adminsList = this.usersService.findCondListAdminsService(pageInfoUtil, condMap);
		request.setAttribute("adminsList", adminsList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		// 存储条件
		request.setAttribute("keyword", keyword);
		request.setAttribute("status", status);
		request.setAttribute("st", st);
		request.setAttribute("ed", ed);
		
		return "/back/adminsList";
	}
	//管理员添加页面
	@RequestMapping("aminsInsert.htm")
	public String adminsInsert(){
		ConstatFinalUtil.LOGGER_SYSTEM.info("--- adminsInsert ---");
		return "back/adminsInsert";
	}
	//打开管理员提交页面
	@RequestMapping(value="adminsInsertSubmit.htm",produces="text/html; charset=utf-8")
	public @ResponseBody String adminsInsertSubmit(AAdmins admins,HttpServletRequest request){
		//设置密码
		admins.setPassword(this.encryptUtil.encryString(admins.getPassword()));
		admins.setCreateTime(new Date());
		admins.setUpdateTime(new Date());
		admins.setLastLoginTime(new Date());
		JSONObject resultJSON=this.usersService.insertOneAdminsService(admins);
		ConstatFinalUtil.LOGGER_SYSTEM.info("---------{}----------"+resultJSON);
		resultJSON=this.returnJuiJSON(request,resultJSON);
		//返回JUI需要的JSON,默认返回字符串
		return resultJSON.toJSONString();
	}
	
	//退出
	@RequestMapping("logout.htm")
	public String logout(HttpServletRequest request){
		//登陆的时候将放入session的信息,remove
		HttpSession session = request.getSession();
		session.removeAttribute("admins");
		session.removeAttribute("lastLoginTime");
		/* 退出客户端跳转 */
		return "redirect:/adminsLogin.htm" ; 
	
	}
}